package com.itheimamobile.controller;

import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetMeal;
import com.itheima.pojo.Setmeal;
import com.itheima.service.MobileOrderSettingService;
import com.itheima.service.SetMealService;
import com.itheima.utils.SMSSendUtil;
import com.itheima.utils.ValidateCodeUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.List;

@RestController
@RequestMapping("/setmeal")
public class MobileSetMealController {

    @Reference
    private SetMealService mobileSetMealService;

    @Autowired
    private JedisPool jedisPool;

    @RequestMapping("/getSetmeal")
    public Result getSetMeal(){
        List<Setmeal> msList =  mobileSetMealService.getAllSetMeal();
        return new Result(true, MessageConstant.GET_SETMEAL_LIST_SUCCESS,msList);
    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        Setmeal setmeal = mobileSetMealService.findById(id);
        return new Result(true,MessageConstant.GET_SETMEAL_LIST_SUCCESS,setmeal);
    }

    /**
     * 发送验证码 ：腾讯云短信 短信预约验证码校验
     * @param telephoneNumber 待发送验证码手机号
     * @return 验证码发送结果 （success or defeat）
     */
    @RequestMapping("/sendSMS")
    public Result sendSMS(String telephoneNumber){

        try{
            //获取 4 位验证码
            Integer valida = ValidateCodeUtils.generateValidateCode(4);
            //发送验证码 预约验证码发送 MODEL_ID_ORDER = **
            //SMSSendUtil.sendShortMessage(SMSSendUtil.MODEL_ID_ORDER,telephoneNumber,valida.toString());
            //为了方便验证 将验证码 存放到 jedis 中 （为了便于区分 利用 常量类区分 SENDTYPE_ORDER = 001）
            //设置五分钟过期时间
            jedisPool.getResource()
                    .setex(telephoneNumber + RedisMessageConstant.SENDTYPE_ORDER,300,valida.toString());

            System.out.println("预约验证码" + valida);
        }catch (Exception ex){
            ex.printStackTrace();
            return new Result(false,null,null);
        }

        return new Result(true,null,null);
    }


}